package com.xbongbong.workflow.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.NodeTypeEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.workflow.domain.dao.WorkflowTaskNodeDao;
import com.xbongbong.workflow.domain.entity.WorkflowTaskNodeEntity;
import com.xbongbong.workflow.domain.entity.ext.WorkflowTaskNodeEntityExt;
import com.xbongbong.workflow.model.WorkflowTaskNodeModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

/**
 */
@Service("workflowTaskNodeModel")
public class WorkflowTaskNodeModelImpl implements WorkflowTaskNodeModel {
    @Resource
    private WorkflowTaskNodeDao workflowTaskNodeDao;

    private static final String COLUMNS = " id, corpid, app_id, menu_id, form_id, workflow_id, task_id, user_id, data_id, task_type, sign_type, node_type, last_node_id, workflow_node_id, opinion, images, attachment, transfer_user_id, revoke_user_id, backspace_node, is_read, add_time, update_time, del, dingtalk_workrecord_activity_id, dingtalk_workrecord_task_id, dingtalk_process_instance_id, title ";

    /**
     * mysql 5.7 默认开启 only_full_group_by 修改列名
     */
    private static final String GROUP_COLUMNS = " any_value(id) id, any_value(corpid) corpid, any_value(app_id) app_id, any_value(menu_id) menu_id, any_value(form_id) form_id, any_value(workflow_id) workflow_id, any_value(task_id) task_id, any_value(user_id) user_id, any_value(data_id) data_id, any_value(task_type) task_type, any_value(sign_type) sign_type, any_value(node_type) node_type, any_value(last_node_id) last_node_id, any_value(workflow_node_id) workflow_node_id, any_value(opinion) opinion, any_value(images) images, any_value(attachment) attachment, any_value(transfer_user_id) transfer_user_id, any_value(revoke_user_id) revoke_user_id, any_value(backspace_node) backspace_node, any_value(is_read) is_read, any_value(add_time) add_time, any_value(update_time) update_time, any_value(del) del, any_value(title) title ";


    @Override
    public Long insert(WorkflowTaskNodeEntity entity) {
        return workflowTaskNodeDao.insert(entity);
    }

    @Override
    public void insertBatch(List<WorkflowTaskNodeEntity> list) {
        workflowTaskNodeDao.insertBatch(list);
    }

    @Override
    public List<WorkflowTaskNodeEntity> findEntities(Map<String, Object> param) {
        return workflowTaskNodeDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return workflowTaskNodeDao.getEntitiesCount(param);
    }

    @Override
    public void update(WorkflowTaskNodeEntity entity) {
        entity.setUpdateTime(DateUtil.getNow());
        workflowTaskNodeDao.update(entity);
    }

    @Override
    public WorkflowTaskNodeEntity getByKey(Long key, String corpid) {
        return workflowTaskNodeDao.getByKey(key, corpid);
    }
    
    @Override
    public void updateBatch(List<WorkflowTaskNodeEntity> list, String corpid) {
        for (WorkflowTaskNodeEntity entity : list) {
            entity.setUpdateTime(DateUtil.getNow());
        }
        workflowTaskNodeDao.updateBatch(list, corpid);
        
    }
    
    @Override
    public List<WorkflowTaskNodeEntityExt> getJoinNodeForList(Map<String, Object> map) {
        return workflowTaskNodeDao.getJoinNodeForList(map);
    }
    
    @Override
    public List<WorkflowTaskNodeEntityExt> findExtEntities(Map<String, Object> param) {
        return workflowTaskNodeDao.findExtEntities(param);
    }
    
    @Override
    public List<WorkflowTaskNodeEntityExt> getLatestNodeForList(Map<String, Object> param) {
        param.put("columns", COLUMNS);
        param.put("groupColumns", GROUP_COLUMNS);
        return workflowTaskNodeDao.getLatestNodeForList(param);
    }
    
    @Override
    public void readAllCcList(HashMap<String, Object> param) {
        workflowTaskNodeDao.readAllCcList(param);
    }

    @Override
    public void deleteByTaskId(Long taskId, String corpid) {
        workflowTaskNodeDao.deleteByTaskId(taskId, corpid);
    }

    @Override
    public void readNode(WorkflowTaskNodeEntity nodeEntity) {
        workflowTaskNodeDao.update(nodeEntity);
    }

    @Override
    public void deleteByTaskIds(String corpid, List<Long> taskIds) {
        workflowTaskNodeDao.deleteByTaskIds(corpid, taskIds);
    }

    @Override
    public WorkflowTaskNodeEntity getWorkflowStartNodeTask(String corpid, Long formId, Long taskId, Long workflowId) {
        // 查找开始节点
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.FORMID, formId);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("taskId", taskId);
        param.put("workflowId", workflowId);
        param.put("nodeType", NodeTypeEnum.START_PROCESS_NODE.getNodeType());
        param.put(ParameterConstant.ORDER_BY_STR, "update_time desc, id desc");
        List<WorkflowTaskNodeEntity> startNodeList = findEntities(param);
        return CollectionsUtil.isEmpty(startNodeList) ? null : startNodeList.get(0);
    }

    @Override
    public Integer getTaskCountLimit(Map<String, Object> param) {
        return workflowTaskNodeDao.getTaskCountLimit(param).size();
    }
}
